# -*- coding: utf-8 -*-

import os # 引入os

import sys # 引入sys

#引入QgsCore模块
from qgis.core import (
    QgsApplication,
    QgsProject,
    QgsCoordinateReferenceSystem,
    QgsMapLayer,
    QgsLayerTreeModel
)

# 引入QgsGui模块
from qgis.gui import (
    QgsLayerTreeView,
    QgsLayerTreeMapCanvasBridge
)

# 引入Qt内容
from PyQt5.QtWidgets import (
    QDockWidget,
    QAbstractItemView
)

from PyQt5.QtCore import (
    Qt
)

from frmApp import MainWindow # 引入UI界面

from src.QgsExtrasTools_hli.UtilsTool.QEMapTools import MapTools # 引入地图工具
from src.QgsExtrasTools_hli.UtilsTool.QELayerTools import LayerTools # 引入图层工具
from src.QgsExtrasTools_hli.Comments.QETocControl import (
    TocControl,
    TocItem,
    QActionType
) # 引入图层控制工具

file_name, _ = os.path.splitext(os.path.basename(__file__)) # 获取当前文件名不带后缀

# 程序入口
# 如果是直接运行该文件，则执行以下代码
if __name__ == "__main__":
    # 创建一个QgsApplication对象
    # QgsApplication对象用于管理应用程序的资源，
    # 并且是所有GUI应用程序的父对象，
    # 它是所有GUI对象的中心，
    # 它管理所有的事件和窗口，
    # 并且是所有GUI对象的父对象。

    app = QgsApplication([], True)

    app.initQgis() # 初始化QGIS

    crs = QgsCoordinateReferenceSystem('EPSG:4326') # 创建坐标参考系统对象
    
    QgsProject.instance().setCrs(crs) # 设置当前地图的坐标参考系统

    mainWin = MainWindow(file_name) # 初始化窗口

    maptool = MapTools() # 初始化地图工具

    mainWin.addWidget(maptool.toMapCanvas()) # 添加地图工具到窗口

    layer_manage_dockWidget = QDockWidget("Layer Manage") # 初始化QDockWidget

    layer_manage_toccontrol = TocControl(layer_manage_dockWidget) # 初始化图层管理列表
    layer_manage_toccontrol.AddAction(QActionType.ALL,"缩放至图层")
    layer_manage_toccontrol.AddAction(QActionType.Vector,"打开属性表")
    layer_manage_toccontrol.AddAction(QActionType.Raster,"设置拉伸参数")
    layer_manage_toccontrol.ShowMenuTip(True)
    layer_manage_dockWidget.setWidget(layer_manage_toccontrol) #将treeView绑定到dockwidget中
    
    def layer_control_visiable(item):
        if item.flags() & Qt.ItemIsUserCheckable:
            if isinstance(item,TocItem):
                q_item:TocItem = item
                layer:QgsMapLayer = q_item.GetObject()
                maptool.setLayerVisible(layer,item.checkState() == Qt.Checked)
    layer_manage_toccontrol.itemChanged.connect(layer_control_visiable)
    
    def layer_move(start:int,end:int):
        item = layer_manage_toccontrol.item(end)
        if isinstance(item,TocItem):
            q_item:TocItem = item
            layer:QgsMapLayer = q_item.GetObject()
            
            if start < end:
                maptool.moveLayer(layer,end + 1)
            else:
                maptool.moveLayer(layer,end)
    layer_manage_toccontrol.rowsMove.connect(layer_move)


    # 绑定QgsLayerTreeMapCanvasBridge
    layer_manage_bridge = QgsLayerTreeMapCanvasBridge(QgsProject.instance().layerTreeRoot(), maptool.toMapCanvas(),mainWin)

    mainWin.addDockWidget(layer_manage_dockWidget)


    layer_manage_dockWidget = QDockWidget("Layer Manage2") # 初始化QDockWidget

    layer_manage_treeview = QgsLayerTreeView(mainWin) # 初始化treeview
    layer_manage_model = QgsLayerTreeModel(QgsProject.instance().layerTreeRoot(),mainWin) #初始化模型
    layer_manage_model.setFlag(QgsLayerTreeModel.Flag.AllowNodeRename)
    layer_manage_model.setFlag(QgsLayerTreeModel.Flag.AllowNodeReorder)
    layer_manage_model.setFlag(QgsLayerTreeModel.Flag.AllowNodeChangeVisibility)
    layer_manage_model.setFlag(QgsLayerTreeModel.Flag.ShowLegendAsTree)
    layer_manage_treeview.setModel(layer_manage_model) # 将模型绑定到treeview
    layer_manage_dockWidget.setWidget(layer_manage_treeview) #将treeView绑定到dockwidget中
    mainWin.addDockWidget(layer_manage_dockWidget)


    vec_layer = LayerTools.load_vector_layer(r"D:\data\VectorData\2015年全国县级矢量数据\CHN_adm0.shp")
    vec_layer2 = LayerTools.load_vector_layer(r"D:\data\VectorData\2015年全国县级矢量数据\CHN_adm2.shp")
    maptool.addLayer(vec_layer)
    layer_manage_toccontrol.insertItem(0,TocItem(vec_layer.name(),LayerTools.get_layer_icon(vec_layer),vec_layer))

    maptool.addLayer(vec_layer2)
    layer_manage_toccontrol.insertItem(0,TocItem(vec_layer2.name(),LayerTools.get_layer_icon(vec_layer2),vec_layer2))

    mainWin.show()

    sys.exit(app.exec_())